﻿/*----------------------------------------------------------------
//  <copyright file="RepositoryExtensions.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2024-02-06 10:15</last-date>
//----------------------------------------------------------------*/

using System.Linq.Dynamic.Core;

namespace MicroCloud.Entity
{
    #region "DbSet 实例 <see cref="IQueryable{T}"/> 扩展辅助操作"
    /// <summary>
    /// DbSet 实例 <see cref="IQueryable{T}"/> 扩展辅助操作
    /// </summary>
    public static class QueryableExtensions
    {
        #region "扩展方法"
        #region "以字符串方式组装 DbSet 实例 <see cref="IQueryable{T}"/> Where 部分表达式"
        /// <summary>
        /// 以字符串方式组装 DbSet 实例 <see cref="IQueryable{T}"/> Where部分表达式
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="queryable">DbSet 实例 <see cref="IQueryable{T}"/></param>
        /// <param name="wheres">
        /// Where 部分表达式字符串
        /// <para>例："Field1.Contains(\"{value}\") and Field2.Equals(\"{value}\") and Field3 = {value}"，可传入数组自动拼接。</para>
        /// </param>
        /// <returns>DbSet 实例 <see cref="IQueryable{T}"/></returns>
        public static IQueryable<T> WhereAsString<T>(this IQueryable<T> queryable, params string[] wheres)
        {
            if (queryable != null && wheres?.Length > 0)
            {
                string predicate = $"{wheres.ExpandAndToString(" ")}";

                queryable = queryable.Where(predicate);
            }
            return queryable;
        }
        #endregion
        #region "以字符串方式组装 DbSet 实例 <see cref="IQueryable{T}"/> Select部分表达式"
        /// <summary>
        /// 以字符串方式组装 DbSet 实例 <see cref="IQueryable{T}"/> Select部分表达式
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="queryable">DbSet 实例 <see cref="IQueryable{T}"/></param>
        /// <param name="selects">Select 部分表达式字符串</param>
        /// <returns>DbSet 实例 <see cref="IQueryable{T}"/></returns>
        public static IQueryable<T> SelectAsString<T>(this IQueryable<T> queryable, params string[] selects)
        {
            if (queryable != null)
            {
                if (selects?.Length > 0)
                {
                    string selector;
                    if (selects.Length == 1)
                    {
                        selector = selects[0];
                        //.Select("Id");
                    }
                    else
                    {
                        selector = $"new ({selects.ExpandAndToString(",")})";
                        //.Select("new (it.Key as IsAdult,it as UserInfos)");
                    }
                    queryable = queryable.Select<T>(selector);
                    //queryableActuator = queryable.Select(selectorString);
                }
            }
            return queryable;
        }
        #endregion

        #endregion

    }
    #endregion

}
